home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mint99s / signal.c < prev    next >
C/C++ Source or Header  |  1992-12-23  |  17KB  |  614 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* signal.c:: signal handling routines */
  8.  
  9. #include "mint.h"
  10.  
  11. void (*sig_routine)();    /* used in intr.s */
  12. short sig_exc;        /* used in intr.s */
  13.  
  14. /*
  15.  * killgroup(pgrp, sig): send a signal to all members of a process group
  16.  * returns 0 on success, or an error code on failure
  17.  */
  18.  
  19. long
  20. killgroup(pgrp, sig)
  21.     int pgrp, sig;
  22. {
  23.     PROC *p;
  24.     int found = 0;
  25.  
  26.     TRACE(("killgroup %d %d", pgrp, sig));
  27.  
  28.     if (pgrp < 0)
  29.         return EINTRN;
  30.  
  31.     for (p = proclist; p; p = p->gl_next) {
  32.         if (p->pgrp == pgrp) {
  33.             post_sig(p, sig);
  34.             found++;
  35.         }
  36.     }
  37.     if (found) {
  38.         check_sigs();    /* see if the current process is affected */
  39.         return 0;
  40.     }
  41.     else {
  42.         DEBUG(("killgroup: no processes found"));
  43.         return EFILNF;
  44.     }
  45. }
  46.  
  47. /* post_sig: post a signal as being pending. It is assumed that the
  48.    caller has already verified that "sig" is a valid signal, and
  49.    moreover it is the caller's responsibility to call check_sigs()
  50.    if it's possible that p == curproc
  51.  */
  52.  
  53. void
  54. post_sig(p, sig)
  55.     PROC *p;
  56.     int sig;
  57. {
  58.     ulong sigm;
  59.  
  60. /* if process is ignoring this signal, do nothing
  61.  * also: signal 0 is SIGNULL, and should never be delivered through
  62.  * the normal channels (indeed, it's filtered out in dossig.c,
  63.  * but the extra sanity check here is harmless). The kernel uses
  64.  * signal 0 internally for some purposes, but it is handled
  65.  * specially (see supexec() in xbios.c, for example).
  66.  */
  67.     if (p->sighandle[sig] == SIG_IGN || sig == 0)
  68.         return;
  69.  
  70. /* if the process is already dead, do nothing */
  71.     if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  72.         return;
  73.  
  74. /* mark the signal as pending */
  75.     sigm = (1L << (unsigned long)sig);
  76.     p->sigpending |= sigm;
  77.  
  78. /* if the signal is masked, do nothing further */
  79.     if ( (p->sigmask & sigm) != 0 )
  80.         return;
  81.  
  82. /* otherwise, make sure the process is awake */
  83.     if (p->wait_q && p->wait_q != READY_Q) {
  84.         short sr = spl7();
  85.         rm_q(p->wait_q, p);
  86.         add_q(READY_Q, p);
  87.         spl(sr);
  88.     }
  89. }
  90.  
  91. /*
  92.  * check_sigs: see if we have any signals pending. if so,
  93.  * handle them.
  94.  */
  95.  
  96. void
  97. check_sigs()
  98. {
  99.     ulong sigs, sigm;
  100.     int i;
  101.     short deliversig;
  102.  
  103.     if (curproc->pid == 0) return;
  104. top:
  105.     sigs = curproc->sigpending & ~(curproc->sigmask);
  106.     if (sigs) {
  107.         sigm = 2;
  108. /* with tracing we need a mechanism to allow a signal to be delivered
  109.  * to the child (curproc); Fcntl(...TRACEGO...) passes a SIGNULL to indicate that we
  110.  * should really deliver the signal, hence its always safe to remove it
  111.  * from pending.
  112.  */
  113.         deliversig = (curproc->sigpending & 1L);
  114.         curproc->sigpending &= ~1L;
  115.  
  116.         for (i = 1; i < NSIG; i++) {
  117.             if (sigs & sigm) {
  118.                 curproc->sigpending &= ~sigm;
  119.                 if (curproc->ptracer && !deliversig) {
  120.                     TRACE(("tracer being notified of signal %d", i));
  121.                     stop(i);
  122.         /* the parent may reset our pending signals, so check again */
  123.                     goto top;
  124.                 } else {
  125.                     ulong omask;
  126.  
  127.                     curproc->sigpending &= ~sigm;
  128.                     omask = curproc->sigmask;
  129.  
  130.         /* sigextra gives which extra signals should also be masked */
  131.                     curproc->sigmask |= curproc->sigextra[i] | sigm;
  132.                     handle_sig(i);
  133.  
  134.  
  135. /*
  136.  * POSIX.1-3.3.4.2(723) "If and when the user's signal handler returns
  137.  * normally, the original signal mask is restored."
  138.  *
  139.  * BUG?: This unmasking could unmask a pending signal which we will not
  140.  * see this time around (if the signal number is less than i) and which
  141.  * was not pending when we started; should we detect this condition and
  142.  * loop around for a second try? POSIX only guarantees delivery of
  143.  * one signal per kernel entry, so this shouldn't really be a problem.
  144.  */
  145.                     curproc->sigmask = omask;    /* unmask signals */
  146.                 }
  147.             }
  148.             sigm = sigm << 1;
  149.         }
  150.     }
  151. }
  152.  
  153. /*
  154.  * raise: cause a signal to be raised in the current process
  155.  */
  156.  
  157. void
  158. raise(sig)
  159.     int sig;
  160. {
  161.     post_sig(curproc, sig);
  162.     check_sigs();
  163. }
  164.  
  165. #ifdef EXCEPTION_SIGS
  166. /* exception numbers corresponding to signals */
  167. char excep_num[NSIG] =
  168. { 0, 0, 0, 0,
  169.   4,            /* SIGILL == illegal instruction */
  170.   9,            /* SIGTRAP == trace trap    */
  171.   4,            /* pretend SIGABRT is also illegal instruction */
  172.   8,            /* SIGPRIV == privileged instruction exception */
  173.   5,            /* SIGFPE == divide by zero */
  174.   0, 2,            /* SIGBUS == bus error */
  175.   3            /* SIGSEGV == address error */
  176. /* everything else gets zeros */
  177. };
  178.  
  179. /* a "0" means we don't print a message when it happens -- typically the
  180.    user is expecting a synchronous signal, so we don't need to report it
  181. */
  182.  
  183. const char *signames[NSIG] = { 0,
  184. 0, 0, 0, "ILLEGAL INSTRUCTION", "TRACE TRAP",
  185. 0, "PRIVILEGE VIOLATION", "DIVISION BY ZERO", 0, "BUS ERROR",
  186. "ADDRESS ERROR", "BAD SYSTEM CALL", 0, 0, 0,
  187. 0, 0, 0, 0, 0,
  188. 0, 0, 0, "CPU TIME EXHAUSTED", "FILE TOO BIG",
  189. 0, 0, 0, 0, 0
  190. };
  191.  
  192. /*
  193.  * replaces the TOS "show bombs" routine: for now, print the name of the
  194.  * interrupt on the console, and save info on the crash in the appropriate
  195.  * system area
  196.  */
  197.  
  198. void
  199. bombs(sig)
  200.     int sig;
  201. {
  202.     long *procinfo = (long *)0x380L;
  203.     int i;
  204.     CONTEXT *crash;
  205.     extern int no_mem_prot;
  206.  
  207.     if (!no_mem_prot && sig == SIGBUS) {
  208.         /* already reported by report_buserr */
  209.     }
  210.     else if (sig < 0 || sig > 31) {
  211.         ALERT("bombs(%d): sig out of range", sig);
  212.     }
  213.     else if (signames[sig]) {
  214.         ALERT("%s: User PC=%lx (basepage=%lx)",
  215.             signames[sig],
  216.             curproc->exception_pc, curproc->base);
  217.  
  218. /* save the processor state at crash time */
  219. /* assumes that "crash time" is the context curproc->ctxt[SYSCALL] */
  220. /* BUG: this is not true if the crash happened in the kernel; in the
  221.  * latter case, the crash context wasn't saved anywhere.
  222.  */
  223.         crash = &curproc->ctxt[SYSCALL];
  224.         *procinfo++ = 0x12345678L;    /* magic flag for valid info */
  225.         for (i = 0; i < 15; i++)
  226.             *procinfo++ = crash->regs[i];
  227.         *procinfo++ = curproc->exception_ssp;
  228.         *procinfo++ = ((long)excep_num[sig]) << 24L;
  229.         *procinfo = crash->usp;
  230.  
  231. /* we're also supposed to save some info from the supervisor stack. it's not
  232.  * clear what we should do for MiNT, since most of the stuff that used to be
  233.  * on the stack has been put in the CONTXT struct. Moreover, we don't want
  234.  * to crash because of an attempt to access illegal memory. Hence, we do
  235.  * nothing here...
  236.  */
  237.     } else {
  238.         TRACE(("bombs(%d)", sig));
  239.     }
  240. }
  241. #endif
  242.  
  243. /*
  244.  * handle_sig: do whatever is appropriate to handle a signal
  245.  */
  246.  
  247. static long unwound_stack = 0;
  248.  
  249. void
  250. handle_sig(sig)
  251.     int sig;
  252. {
  253.     long oldstack, newstack;
  254.     long *stack;
  255.     CONTEXT *call, oldsysctxt, newcurrent;
  256.     extern void sig_return();
  257.  
  258.     if (curproc->sighandle[sig] == SIG_IGN)
  259.         return;
  260.     else if (curproc->sighandle[sig] == SIG_DFL) {
  261. _default:
  262.         switch(sig) {
  263. #if 0
  264. /* Note: SIGNULL is filtered out in dossig.c and is never actually
  265.  * delivered (its only purpose for the user is to test for the existence of
  266.  * a process, it isn't a real signal). The kernel uses SIGNULL
  267.  * internally, but all such code does the signal handling "by hand"
  268.  * and so no default handling is necessary.
  269.  */
  270.         case SIGNULL:
  271. #endif
  272.         case SIGWINCH:
  273.         case SIGCHLD:
  274. /* SIGFPE is divide by 0; TOS ignores this, so we will too */
  275.         case SIGFPE:
  276.             return;        /* do nothing */
  277.         case SIGSTOP:
  278.         case SIGTSTP:
  279.         case SIGTTIN:
  280.         case SIGTTOU:
  281.             stop(sig);
  282.             return;
  283.         case SIGCONT:
  284.             curproc->sigpending &= ~STOPSIGS;
  285.             return;
  286.  
  287. /* here are the fatal signals. for SIGINT, we use p_term(-32) so that
  288.  * TOS programs that catch ^C via the vector at 0x400 and which expect
  289.  * TOS's error code (-32) to be sent will work. For most other signals,
  290.  * we p_term with an error code; for SIGKILL, we don't want to allow
  291.  * the program any chance to recover, so we call terminate() directly
  292.  * to avoid calling through to the user's terminate vector.
  293.  */
  294.         case SIGINT:        /* ^C */
  295.             if (curproc->domain == DOM_TOS) {
  296.                 p_term(-32);
  297.                 return;
  298.             }
  299.             /* otherwise, fall through */
  300.         default:
  301. #ifdef EXCEPTION_SIGS
  302.             bombs(sig); /* tell the user what happened */
  303. #endif
  304.     /* the "sigmask" check is in case a bus error happens in the user's
  305.      * term_vec code; we don't want to get stuck in an infinite loop!
  306.      */
  307.             if ((curproc->sigmask & 1L) || sig == SIGKILL)
  308.                 terminate(sig << 8, ZOMBIE_Q);
  309.             else
  310.                 p_term(sig << 8);
  311.         }
  312.     }
  313.     else {        /* user wants to handle it himself */
  314.  
  315. /* another kludge: there is one case in which the p_sigreturn mechanism
  316.  * is invoked by the kernel, namely when the user calls Supexec()
  317.  * or when s/he installs a handler for the GEMDOS terminate vector (#0x102)
  318.  * and the program terminates. MiNT fakes the call to user code with
  319.  * signal 0 (SIGNULL); programs that longjmp out of the user function
  320.  * and are later sent back to it again (e.g. if ^C keeps getting pressed
  321.  * and a terminate vector has been installed) will grow the stack without
  322.  * bound unless we watch for this case.
  323.  *
  324.  * Solution (sort of): whenever Pterm() is called, we unwind the
  325.  * stack; otherwise, we let it grow, so that nested Supexec()
  326.  * calls work.
  327.  *
  328.  * Note that SIGNULL is thrown away when sent by user processes, 
  329.  * and the user can't mask it (it's UNMASKABLE), so there is
  330.  * is no possibility of confusion with anything the user does.
  331.  */
  332.         if (sig == 0) {
  333.     /* p_term() sets sigmask to let us know to do Psigreturn */
  334.             if (curproc->sigmask & 1L) {
  335.                 p_sigreturn();
  336.                 curproc->sigmask &= ~1L;
  337.             } else {
  338.                 unwound_stack = 0;
  339.             }
  340.         }
  341.  
  342.         call = &curproc->ctxt[SYSCALL];
  343. /*
  344.  * what we do is build two fake stack frames; the bottom one is
  345.  * for a call to the user function, with (long)parameter being the
  346.  * signal number; the top one is for sig_return.
  347.  * When the user function returns, it returns to sig_return, which
  348.  * calls into the kernel to restore the context in prev_ctxt
  349.  * (thus putting us back here). We can then continue on our way.
  350.  */
  351.  
  352. /* set a new system stack, with a bit of buffer space */
  353.         oldstack = curproc->sysstack;
  354.         newstack = ((long) ( (&newcurrent) - 3 )) - 12;
  355.  
  356.         if (newstack < (long)curproc->stack + ISTKSIZE + 256) {
  357.             ALERT("stack overflow");
  358.             goto _default;
  359.         }
  360.         else if ((long) curproc->stack + STKSIZE < newstack) {
  361.             FATAL("system stack not in proc structure");
  362.         }
  363.  
  364. /* unwound_stack is set by p_sigreturn() */
  365.         if (sig == 0 && unwound_stack)
  366.             curproc->sysstack = unwound_stack;
  367.         else
  368.             curproc->sysstack = newstack;
  369.         oldsysctxt = *call;
  370.         stack = (long *)(call->sr & 0x2000 ? call->ssp :
  371.                 call->usp);
  372. /*
  373.    Hmmm... here's another potential problem for the signal 0 terminate
  374.    vector: if the program keeps returning back to user mode without
  375.    worrying about the supervisor stack, we'll eventually overflow it.
  376.    However, if the program is in supervisor mode itself, then we don't
  377.    want to stomp on its stack. Temporary solution: ignore the problem,
  378.    the stack's only growing 12 bytes at a time.
  379.  */
  380. /*
  381.  * in addition to the signal number we stuff the vector offset on the
  382.  * stack; if the user is interested they can sniff it, if not ignoring
  383.  * it needs no action on their part. Why do we need this? So that a
  384.  * single SIGFPE handler (for example) can discriminate amongst the
  385.  * multiple things which may get thrown its way
  386.  */
  387.         *(--stack) = (long)call->sfmt & 0xfff;
  388.         *(--stack) = (long)sig;
  389.         *(--stack) = (long)sig_return;
  390.         if (call->sr & 0x2000)
  391.             call->ssp = ((long) stack);
  392.         else
  393.             call->usp = ((long) stack);
  394.         call->pc = (long) curproc->sighandle[sig];
  395.         call->sfmt = call->fstate[0] = 0;    /* don't restart FPU communication */
  396.  
  397.         ((long *)curproc->sysstack)[1] = FRAME_MAGIC;
  398.         ((long *)curproc->sysstack)[2] = oldstack;
  399.         ((long *)curproc->sysstack)[3] = sig;
  400.  
  401.         if (curproc->sigflags[sig] & SA_RESET) {
  402.             curproc->sighandle[sig] = SIG_DFL;
  403.             curproc->sigflags[sig] &= ~SA_RESET;
  404.         }
  405.             
  406.         if (save_context(&newcurrent) == 0 ) {
  407. /*
  408.  * go do the signal; eventually, we'll restore this context (unless the
  409.  * user longjmp'd out of his signal handler). while the user is handling
  410.  * the signal, it's masked out to prevent race conditions. p_sigreturn()
  411.  * will unmask it for us when the user is finished.
  412.  */
  413.             newcurrent.regs[0] = CTXT_MAGIC;
  414.                 /* set D0 so next return is different */
  415.             assert(curproc->magic == CTXT_MAGIC);
  416.             leave_kernel();
  417.             restore_context(call);
  418.         }
  419. /*
  420.  * OK, we get here from p_sigreturn, via the user returning from
  421.  * the handler to sig_return. Restoring the stack and unmasking the
  422.  * signal have been done already for us by p_sigreturn.
  423.  * We should just restore the old system call context
  424.  * and continue with whatever it was we were doing.
  425.  */
  426.         TRACE(("done handling signal"));
  427.         curproc->ctxt[SYSCALL] = oldsysctxt;
  428.         assert(curproc->magic == CTXT_MAGIC);
  429.     }
  430. }
  431.  
  432. /*
  433.  * the p_sigreturn system call
  434.  * When called by the user from inside a signal handler, it indicates a
  435.  * desire to restore the old stack frame prior to a longjmp() out of
  436.  * the handler.
  437.  * When called from the sig_return module, it indicates that the user
  438.  * is finished a handler, and we should not only restore the stack
  439.  * frame but also the old context we were working in (which is on the
  440.  * system call stack -- see handle_sig).
  441.  * The "valid_return" variable is 0 in the first case, 1 in the second.
  442.  */
  443.  
  444. short valid_return;
  445.  
  446. long ARGS_ON_STACK
  447. p_sigreturn()
  448. {
  449.     CONTEXT *oldctxt;
  450.     long *frame;
  451.     long sig;
  452.  
  453.     unwound_stack = 0;
  454. top:
  455.     frame = (long *)curproc->sysstack;
  456.     frame++;    /* frame should point at FRAME_MAGIC, now */
  457.     sig = frame[2];
  458.     if (*frame != FRAME_MAGIC || (sig < 0) || (sig >= NSIG)) {
  459.         FATAL("Psigreturn: system stack corrupted");
  460.     }
  461.     if (frame[1] == 0) {
  462.         DEBUG(("Psigreturn: frame at %lx points to 0", frame-1));
  463.         return 0;
  464.     }
  465.     unwound_stack = curproc->sysstack;
  466.     TRACE(("Psigreturn(%d)", (int)sig));
  467.  
  468.     curproc->sysstack = frame[1];    /* restore frame */
  469.     curproc->sigmask &= ~(1L<<sig); /* unblock signal */
  470.  
  471.     if (!valid_return) {
  472. /* here, the user is telling us that a longjmp out of a signal handler is
  473.  * about to occur; so we should unwind *all* the signal frames
  474.  */
  475.         goto top;
  476.     }
  477.     else {
  478.         valid_return = 0;
  479.         oldctxt = ((CONTEXT *)(&frame[2])) + 3;
  480.         if (oldctxt->regs[0] != CTXT_MAGIC) {
  481.             FATAL("p_sigreturn: corrupted context");
  482.         }
  483.         assert(curproc->magic == CTXT_MAGIC);
  484.         restore_context(oldctxt);
  485.         return 0;    /* dummy -- this isn't reached */
  486.     }
  487. }
  488.  
  489. /*
  490.  * stop a process because of signal "sig"
  491.  */
  492.  
  493. void
  494. stop(sig)
  495.     int sig;
  496. {
  497.     unsigned int code;
  498.     unsigned long oldmask;
  499.     PROC *p;
  500.  
  501.     code = sig << 8;
  502.  
  503.     if (curproc->pid == 0) {
  504.         FORCE("attempt to stop MiNT");
  505.         return;
  506.     }
  507.  
  508. /* notify parent */
  509.     if (curproc->ptracer) {
  510.         p = curproc->ptracer;
  511.         post_sig(p, SIGCHLD);
  512.     } else {
  513.         p = pid2proc(curproc->ppid);
  514.         if (p && !(p->sigflags[SIGCHLD] & SA_NOCLDSTOP))
  515.             post_sig(p, SIGCHLD);
  516.     }
  517.  
  518.     oldmask = curproc->sigmask;
  519.  
  520.     if ((1L << sig) & STOPSIGS) {
  521.         /* mask out most signals */
  522.         curproc->sigmask |= ~(UNMASKABLE | SIGTERM);
  523.     }
  524.     else
  525.         assert(curproc->ptracer);
  526.  
  527. /* sleep until someone signals us awake */
  528.     sleep(STOP_Q, (long) code | 0177);
  529.  
  530. /* when we wake up, restore the signal mask */
  531.     curproc->sigmask = oldmask;
  532.  
  533. /* and discard any signals that would cause us to stop again */
  534.     curproc->sigpending &= ~STOPSIGS;
  535. }
  536.  
  537. /*
  538.  * interrupt handlers to raise SIGBUS, SIGSEGV, etc. Note that for
  539.  * really fatal errors we reset the handler to SIG_DFL, so that
  540.  * a second such error kills us
  541.  */
  542.  
  543. void
  544. exception(sig)
  545.     int sig;
  546. {
  547.     curproc->sigflags[sig] |= SA_RESET;
  548.     DEBUG(("exception #%d raised", sig));
  549.     raise(sig);
  550. }
  551.  
  552. void
  553. sigbus()
  554. {
  555.     report_buserr();
  556.     exception(SIGBUS);
  557. }
  558.  
  559. void
  560. sigaddr()
  561. {
  562.     exception(SIGSEGV);
  563. }
  564.  
  565. void
  566. sigill()
  567. {
  568.     exception(SIGILL);
  569. }
  570.  
  571. void
  572. sigpriv()
  573. {
  574.     raise(SIGPRIV);
  575. }
  576.  
  577. void
  578. sigfpe()
  579. {
  580.     extern short fpu;    /* in main.c */
  581.     
  582.     if (fpu) {
  583.         CONTEXT *ctxt;
  584.  
  585.         ctxt = &curproc->ctxt[SYSCALL];
  586.  
  587.     /* 0x1f38 is a Motorola magic cookie to detect a 68882 idle state frame */
  588.         if (*(ushort *)ctxt->fstate == 0x1f38 && 
  589.             (ctxt->sfmt & 0xfff) >= 0xc0L && (ctxt->sfmt & 0xfff) <= 0xd8L) {
  590.             /* fix a bug in the 68882 - Motorola call it a feature :-) */
  591.             ctxt->fstate[ctxt->fstate[1]] |= 1 << 3;
  592.         }
  593.     }
  594.     raise(SIGFPE);
  595. }
  596.  
  597. void
  598. sigtrap()
  599. {
  600.     raise(SIGTRAP);
  601. }
  602.  
  603. void
  604. haltformat()
  605. {
  606.     FATAL("halt: invalid stack frame format");
  607. }
  608.  
  609. void
  610. haltcpv()
  611. {
  612.     FATAL("halt: coprocessor protocol violation");
  613. }
  614.